package com.th.supcom.pds.impl.service.internal.phamapply.command;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.carefx.framework.util.ConvertUtils;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.common.enums.InpDispenseIndicator;
import com.th.supcom.pds.impl.common.enums.InpWithDrawType;
import com.th.supcom.pds.impl.common.enums.PhamIoSort;
import com.th.supcom.pds.medicare.api.AdapterDrugManagerService;

/**
 * 药品入库操作
 * 
 * @author leaqua@qq.com
 */
public class PhamStockInWithoutDrugOperation extends AbstractPhamApplyManagerBean implements Command
{

    @Override
    public boolean execute (Context arg0) throws Exception
    {
        PhamDispenseContext phamDispenseContext = (PhamDispenseContext) arg0;
        List <InpPhamApplyDetailVO> inpPhamApplyDetailVOs = phamDispenseContext.getApplyDetailVOList ();
        List <InpPhamDispenseDetailVO> inpPhamDispenseDetailVOs = ConvertUtils.convert (InpPhamDispenseDetailVO.class, inpPhamApplyDetailVOs);
       
        String operator = phamDispenseContext.getOperator ();
        String nurseUnit = phamDispenseContext.getNurseUnitCode ();
        Map <String, Object> map = new HashMap <String, Object> ();

        map.put ("operator", operator);
        map.put ("nurseUnit", nurseUnit);
        phamDispenseContext.setWithdrawType (InpWithDrawType.ReturnWithoutDrug.getEnumCode ());
        AdapterDrugManagerService adapterDrugManagerService = getAdapterDrugManagerService ();
        String exNo = adapterDrugManagerService.inpDrugStockIn (inpPhamApplyDetailVOs, map, PhamIoSort.MactchIn.getEnumCode ());
        // TODO 调用DMS 成功则 像 摆药明细 填写出入库单号
        for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : inpPhamDispenseDetailVOs)
        {
            inpPhamDispenseDetailVO.setDispenseIndicator (InpDispenseIndicator.HasDrugReturn.getEnumCode ());
            inpPhamDispenseDetailVO.setStockNo (exNo);
        }
        for (InpPhamApplyDetailVO inpPhamApplyDetailVO : inpPhamApplyDetailVOs)
        {
            inpPhamApplyDetailVO.setDispenseIndicator (InpDispenseIndicator.HasDrugReturn.getEnumCode ());
        }
        phamDispenseContext.setInpPhamDispenseDetailVOList (inpPhamDispenseDetailVOs);
        return false;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamStockInWithoutDrugOperation.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
